package com.sefer.dragonfly.client.core.domain;

import javax.management.MBeanOperationInfo;
import javax.management.Notification;

import com.sefer.dragonfly.client.core.domain.mbean.annotation.CMbeanAttribute;
import com.sefer.dragonfly.client.core.domain.mbean.annotation.CMbeanMethod;
import com.sefer.dragonfly.client.core.domain.scheduler.JobAdaptor;
import com.sefer.dragonfly.client.utils.MBeanAnnotationRegister;

public abstract class BaseConsumer<K, V> extends JobAdaptor<K, V> implements
		CmConsumer<K, V> {

	private NameSpace namespace;

	@CMbeanAttribute(description = "plugin id")
	protected String id;
	@CMbeanAttribute(description = "plugin version")
	protected String version;
	@CMbeanAttribute(description = "plugin needNotify", isIs = true)
	protected boolean needNotify;
	@CMbeanAttribute(description = "plugin priority")
	protected int priority = 5;
	@CMbeanAttribute(description = "plugin run period expression")
	protected String cronExpression = "0 0/1 * * * ?";
	@CMbeanAttribute(description = "plugin cacheId")
	protected String cacheId;
	@CMbeanAttribute(description = "plugin needCache")
	protected boolean needCache = true;

	public NameSpace getNameSpace() {
		return namespace;
	}

	public String getConsumerId() {
		return id;
	}

	public String getVersion() {
		return version;
	}

	public void setNameSpace(String namespace) {
		this.namespace = new NameSpace(namespace);
	}

	public void setNotifyServer(boolean needNotify) {
		this.needNotify = needNotify;
	}

	public void setConsumerId(String id) {
		this.id = id;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public boolean isNotifyServer() {
		return needNotify;
	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public String getCronExpression() {
		return cronExpression;
	}

	public void setCronExpression(String cronExpression) {
		this.cronExpression = cronExpression;
	}

	public String getCacheId() {
		return this.cacheId;
	}

	public void setCacheId(String id) {
		this.cacheId = id;
	}

	public boolean isNeedCache() {
		return needCache;
	}

	public void setNeedCache(boolean needCache) {
		this.needCache = needCache;
	}

	// ============================>>>>>>>>>>>>>

	/**
	 * 销毁插件，如清空初始化数据，释放所有的数据，并且从监控容器插件注册中心中删除
	 */
	@CMbeanMethod(description = "BaseCPlugin plugin", impact = MBeanOperationInfo.ACTION_INFO)
	public void destory() {
		super.destory();
		// 从mbeanServer中注销
		MBeanAnnotationRegister.unRegister(this);
	}

	/**
	 * 初始化插件
	 */
	@CMbeanMethod(description = "BaseCPlugin init", impact = MBeanOperationInfo.ACTION_INFO)
	public void init() {
		super.init();
	}

	/**
	 * 启动插件，注册到mbeanServer
	 */
	@CMbeanMethod(description = "BaseCPlugin start", impact = MBeanOperationInfo.ACTION_INFO)
	public void start() {
		super.start();
		// 注册到mbeanServer
		MBeanAnnotationRegister.register(this);
	}

	/**
	 * 暂停插件，从定时器容器中注销(由容器完成)
	 */
	@CMbeanMethod(description = "BaseCPlugin stop", impact = MBeanOperationInfo.ACTION_INFO)
	public void stop() {
		super.stop();
		// 个性化定义
	}

	public int compareTo(CmConsumer arg0) {
		return this.getPriority() - arg0.getPriority();
	}

	/**
	 * 处理mbean notify发送的消息
	 */
	public void handleNotification(Notification notification, Object handback) {
	}

	/**
	 * 如果需要，请重载
	 */
	public void beforeExec() {
	}

	/**
	 * 如果需要，请重载
	 */
	public void afterExec() throws Exception {
	}
}
